﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SEO.Business;
using System.Data;

public partial class ArticleTypeReportList : AdminPage
{
    /// <summary>
    /// 用户ID
    /// </summary>
    protected int UserID
    {
        get { return ViewState["UserID"] == null ? 0 : Convert.ToInt32(ViewState["UserID"]); }
        set { ViewState["UserID"] = value; }
    }

    /// <summary>
    /// 名称
    /// </summary>
    protected string Name
    {
        get { return ViewState["Name"] == null ? "" : ViewState["Name"].ToString(); }
        set { ViewState["Name"] = value; }
    }

    /// <summary>
    /// 分类ID
    /// </summary>
    protected int SortID
    {
        get { return ViewState["SortID"] == null ? 0 : Convert.ToInt32(ViewState["SortID"]); }
        set { ViewState["SortID"] = value; }
    }

    /// <summary>
    /// 自动锁定
    /// </summary>
    protected int SysLock
    {
        get { return ViewState["SysLock"] == null ? -1 : Convert.ToInt32(ViewState["SysLock"]); }
        set { ViewState["SysLock"] = value; }
    }

    /// <summary>
    /// 手动锁定
    /// </summary>
    protected int DiyLock
    {
        get { return ViewState["DiyLock"] == null ? -1 : Convert.ToInt32(ViewState["DiyLock"]); }
        set { ViewState["DiyLock"] = value; }
    }

    /// <summary>
    /// 新闻源录入数
    /// </summary>
    protected int BeginNewsIndex
    {
        get { return ViewState["BeginNewsIndex"] == null ? 0 : Convert.ToInt32(ViewState["BeginNewsIndex"]); }
        set { ViewState["BeginNewsIndex"] = value; }
    }

    /// <summary>
    /// 新闻源录入数
    /// </summary>
    protected int EndNewsIndex
    {
        get { return ViewState["EndNewsIndex"] == null ? 99999999 : Convert.ToInt32(ViewState["EndNewsIndex"]); }
        set { ViewState["EndNewsIndex"] = value; }
    }

    /// <summary>
    /// 外链录入数
    /// </summary>
    protected int BeginLinkIndex
    {
        get { return ViewState["BeginLinkIndex"] == null ? 0 : Convert.ToInt32(ViewState["BeginLinkIndex"]); }
        set { ViewState["BeginLinkIndex"] = value; }
    }

    /// <summary>
    /// 外链录入数
    /// </summary>
    protected int EndLinkIndex
    {
        get { return ViewState["EndLinkIndex"] == null ? 99999999 : Convert.ToInt32(ViewState["EndLinkIndex"]); }
        set { ViewState["EndLinkIndex"] = value; }
    }

    /// <summary>
    /// 时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 时间
    /// </summary>
    protected string EndDateTime
    {
        get { return ViewState["EndDateTime"] == null ? "" : ViewState["EndDateTime"].ToString(); }
        set { ViewState["EndDateTime"] = value; }
    }

    protected int nNum = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitSort();
            BindUser();
            InitData();
        }
    }

    /// <summary>
    /// 绑定用户列表
    /// </summary>
    protected void BindUser()
    {
        DataTable dtUser = new BisAdminInfo().GetAllAdminList();
        ddlUser.Items.Clear();
        ddlUser.Items.Add(new ListItem("所有用户", "0"));
        foreach (DataRow drUser in dtUser.Rows)
        {
            ddlUser.Items.Add(new ListItem(drUser["Name"].ToString(), drUser["ID"].ToString()));
        }
        dtUser.Dispose();
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.KeywordsList;
    }

    protected void InitSort()
    {
        DataTable dtSort = GetAllSort();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("所有分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();
        txtBeginTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        BeginDateTime = DateTime.Now.ToString("yyyy-MM-dd");
        txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        EndDateTime = DateTime.Now.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    protected void InitData()
    {
        int nRecordCount = 0;
        int nPageCount = 0;
        DateTime dBeginTime = Convert.ToDateTime(BeginDateTime);
        DateTime dEndTime = Convert.ToDateTime(EndDateTime).AddDays(1).AddSeconds(-1);
        //信息列表绑定
        DataTable dt = new BisKeywordsCount().GetList(Name, UserID, SortID, SysLock, DiyLock, BeginNewsIndex, EndNewsIndex, BeginLinkIndex, EndLinkIndex, dBeginTime, dEndTime, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
       // DataTable dt = new BisKeywords().GetList(Name, SortID, SysLock, DiyLock, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        anpPage.RecordCount = nRecordCount;
        nNum = (anpPage.CurrentPageIndex - 1) * anpPage.PageSize;
        nNum++;
        dt.Columns.Add("AllNewsCount", typeof(int));
        dt.Columns.Add("AllLinkCount", typeof(int));
        dt.Columns.Add("NewsCount", typeof(int));
        dt.Columns.Add("LinkCount", typeof(int));
        if (dt != null && dt.Rows.Count > 0)
        {
            string strIDList = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                strIDList += (string.IsNullOrEmpty(strIDList) ? "" : ",") + dr["ID"].ToString();
            }
            BisArticleSEO bisArticleSEO = new BisArticleSEO();
            //获取提成数
            DataTable dtArticle = bisArticleSEO.GetKeywordsArticleList(strIDList,UserID, true, dBeginTime, dEndTime.AddDays(1));
            if (dtArticle != null && dtArticle.Rows.Count > 0)
            {
                foreach (DataRow drKeywords in dt.Rows)
                {
                    DataRow[] drArticleList = dtArticle.Select("KID=" + drKeywords["ID"].ToString());
                    int nAllNewsCount = 0;
                    int nAllLinkCount = 0;
                    foreach (DataRow drArticle in drArticleList)
                    {
                        switch (Convert.ToInt32(drArticle["UrlType"]))
                        {
                            case 1:
                                nAllNewsCount += Convert.ToInt32(drArticle["AllCount"]);
                                break;
                            case 2:
                                nAllLinkCount += Convert.ToInt32(drArticle["AllCount"]);
                                break;
                        }
                    }
                    drKeywords["NewsCount"] = nAllNewsCount;
                    drKeywords["LinkCount"] = nAllLinkCount;
                }
            }
            dtArticle.Dispose();

            //获取录入数
            DataTable dtAllArticle = bisArticleSEO.GetKeywordsArticleList(strIDList,UserID,1,BeginNewsIndex,EndNewsIndex,dBeginTime,dEndTime.AddDays(1));
            DataTable dtAllArticle1 = bisArticleSEO.GetKeywordsArticleList(strIDList, UserID, 2, BeginLinkIndex, EndLinkIndex, dBeginTime, dEndTime.AddDays(1));
            if (dtAllArticle != null && dtAllArticle.Rows.Count > 0)
            {
                foreach (DataRow drKeywords in dt.Rows)
                {
                    DataRow[] drArticleList = dtAllArticle.Select("KID=" + drKeywords["ID"].ToString());
                    int nAllNewsCount = 0;
                    foreach (DataRow drArticle in drArticleList)
                    {
                        nAllNewsCount += Convert.ToInt32(drArticle["AllCount"]);

                    }
                    drKeywords["AllNewsCount"] = nAllNewsCount;
                }
            }
            dtAllArticle.Dispose();

            if (dtAllArticle1 != null && dtAllArticle1.Rows.Count > 0)
            {
                foreach (DataRow drKeywords in dt.Rows)
                {
                    DataRow[] drArticle1List = dtAllArticle1.Select("KID=" + drKeywords["ID"].ToString());
                    int nAllLinkCount = 0;
                    foreach (DataRow drArticle in drArticle1List)
                    {
                        nAllLinkCount += Convert.ToInt32(drArticle["AllCount"]);

                    }
                    drKeywords["AllLinkCount"] = nAllLinkCount;
                }
            }
            dtAllArticle1.Dispose();
        }


        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptKeywords.DataSource = dt;
        rptKeywords.DataBind();
        dt.Dispose();

        anpPage.CustomInfoHTML = string.Format("<div class=\"pagetag\">当前第<b>{0}</b>/<b>{1}</b>页 共<b>{2}</b>条记录</div>", new object[] { anpPage.CurrentPageIndex, anpPage.PageCount, anpPage.RecordCount });
    }


    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptKeywords_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (nAdminFeatures == 1 || SupportFeatures(AdminFEATURE.KeywordsList))
        {
            if (e.CommandName == "SetDiyLock")
            {
                string[] strItem = e.CommandArgument.ToString().Split(',');
                int nID = Convert.ToInt32(strItem[0]);
                bool bDiyLock = Convert.ToBoolean(strItem[1]);
                new BisKeywords().UpdateDiyLock(nID, !bDiyLock);
            }
            else if (e.CommandName == "SetSysLock")
            {
                string[] strItem = e.CommandArgument.ToString().Split(',');
                int nID = Convert.ToInt32(strItem[0]);
                bool bSysLock = Convert.ToBoolean(strItem[1]);
                new BisKeywords().UpdateSysLock(nID, !bSysLock);
            }
            RemoveKeywords();
            InitData();
        }
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        UserID = Convert.ToInt32(ddlUser.SelectedValue);
        Name = txtName.Text.Trim();
        SortID = Convert.ToInt32(ddlSort.SelectedValue);
        SysLock = Convert.ToInt32(ddlSysLock.SelectedValue);
        DiyLock = Convert.ToInt32(ddlDiyLock.SelectedValue);
        BeginDateTime = txtBeginTime.Text.Trim();
        EndDateTime = txtEndTime.Text.Trim();
        int nBeginNewsIndex = 0;
        int nEndNewsIndex = 99999999;
        int nBeginLinkIndex = 0;
        int nEndLinkIndex = 99999999;
        if (!string.IsNullOrEmpty(txtBeginNewsIndex.Text.Trim()))
        {
            int.TryParse(txtBeginNewsIndex.Text.Trim(), out nBeginNewsIndex);
        }
        if (!string.IsNullOrEmpty(txtEndNewsIndex.Text.Trim()))
        {
            int.TryParse(txtEndNewsIndex.Text.Trim(), out nEndNewsIndex);
            if (nEndNewsIndex == 0)
            {
                nEndNewsIndex = 99999999;
            }
        }
        if (!string.IsNullOrEmpty(txtBeginLinkIndex.Text.Trim()))
        {
            int.TryParse(txtBeginLinkIndex.Text.Trim(), out nBeginLinkIndex);
        }
        if (!string.IsNullOrEmpty(txtEndLinkIndex.Text.Trim()))
        {
            int.TryParse(txtEndLinkIndex.Text.Trim(), out nEndLinkIndex);
            if (nEndLinkIndex == 0)
            {
                nEndLinkIndex = 99999999;
            }
        }
        BeginNewsIndex = nBeginNewsIndex;
        EndNewsIndex = nEndNewsIndex;
        BeginLinkIndex = nBeginLinkIndex;
        EndLinkIndex = nEndLinkIndex;
        anpPage.CurrentPageIndex = 1;
    }
}